package com.ninong.ker.admin.sevice.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ninong.ker.admin.rest.vo.FwFormConfigVO;
import com.ninong.ker.admin.service.dto.FwFormConfigDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ninong.ker.admin.domain.FwFormConfig;
import com.ninong.ker.admin.mapper.FwFormConfigMapper;
import com.ninong.ker.admin.sevice.FwFormConfigService;
import com.ninong.ker.common.exception.CmdException;
import com.ninong.ker.common.tools.Constants;


/**
 * @author jmz
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class FwFormConfigServiceImpl extends ServiceImpl<FwFormConfigMapper, FwFormConfig> implements FwFormConfigService {

	private static final String PARENT = "0";

    @Override
	public PageInfo<FwFormConfigVO> listPage(FwFormConfigDTO dto) {
		PageHelper.startPage(dto.getCurrentPage(), dto.getPageSize());
		List<FwFormConfigVO> formConfigs = baseMapper.selectFormConfigPage(dto);
		return new PageInfo<>(formConfigs);
	}
    
    /**
	 * 查询农事模板list
	 * @param dto
	 * @return
	 */
	@Override
	public List<FwFormConfigVO> findListByDto(FwFormConfigDTO dto){
		List<FwFormConfigVO> formConfigs = baseMapper.selectFormConfigPage(dto);
		return formConfigs;
	}


	@Override
	public void delete(String[] ids) {
		List<String> idList = Arrays.asList(ids);
		idList.forEach(id -> {		
			FwFormConfig entity = new FwFormConfig();
			entity.setId(id);
			baseMapper.updateById(entity);
		});
	}

	@Override
	public void create(FwFormConfig entity) {
		LambdaQueryWrapper<FwFormConfig> query = new LambdaQueryWrapper<>();
		query.eq(FwFormConfig::getFormCode,entity.getFormCode());
		query.eq(FwFormConfig::getLanguage,entity.getLanguage());
		if(StringUtils.isNotEmpty(entity.getId())) {
			//新增的时候校验全数据库
			query.ne(FwFormConfig::getId, entity.getId());
		}
		Long count = baseMapper.selectCount(query);
		if(count > Constants.ZERO){
			throw new CmdException("同一个语言下编号不能重复");
		}
		if(StringUtils.isEmpty(entity.getId())){
			entity.setCreateTime(new Date());
			entity.setUpdateTime(new Date());
		}
		saveOrUpdate(entity);
	}

	@Override
	public FwFormConfig get(String id) {
		FwFormConfig entity = baseMapper.selectById(id);
		if (ObjectUtils.isEmpty(entity)) {
			entity = new FwFormConfig();
		}
		return entity;
	}

	@Override
	public FwFormConfig get(String formCode, String language) {
		LambdaQueryWrapper<FwFormConfig> query = new LambdaQueryWrapper<>();
		query.eq(FwFormConfig::getFormCode,formCode);
		query.eq(FwFormConfig::getLanguage,language);
		FwFormConfig entity = baseMapper.selectOne(query);
		if (ObjectUtils.isEmpty(entity)) {
			entity = new FwFormConfig();
		}
		return entity;
	}

	@Override
	public JSONArray getFeild(String formCode, String language) {
		LambdaQueryWrapper<FwFormConfig> query = new LambdaQueryWrapper<>();
		query.eq(FwFormConfig::getFormCode,formCode);
		query.eq(FwFormConfig::getLanguage,language);
		FwFormConfig entity = baseMapper.selectOne(query);
		String content = entity.getContent();
		List<JSONObject> contents = JSONArray.parseArray(content,JSONObject.class);
		JSONArray jsons = new JSONArray();
		contents.forEach(item ->{
			JSONObject json = new JSONObject();
			json.put("field",item.getString("field"));
			json.put("title",item.getString("title"));
			json.put("type",item.getString("type"));
			jsons.add(json);
		});
		return jsons;
	}

	@Override
	public JSONArray getFeildCondition(String formCode, String language) {
		LambdaQueryWrapper<FwFormConfig> query = new LambdaQueryWrapper<>();
		query.eq(FwFormConfig::getFormCode,formCode);
		query.eq(FwFormConfig::getLanguage,language);
		FwFormConfig entity = baseMapper.selectOne(query);
		String content = entity.getContent();
		List<JSONObject> contents = JSONArray.parseArray(content,JSONObject.class);
		JSONArray jsons = new JSONArray();
		contents.forEach(item ->{
			if(StringUtils.equals("input",item.getString("type"))){
				JSONObject json = new JSONObject();
				json.put("field",item.getString("field"));
				json.put("title",item.getString("title"));
				json.put("type",item.getString("type"));
				jsons.add(json);
			}
		});
		return jsons;
	}

	/**
	 * 产品模板选择上级模板
	 * @param confId
	 * @return
	 */
	@Override
	public List<FwFormConfig> treeForm(String confId) {
		LambdaQueryWrapper<FwFormConfig> query = new LambdaQueryWrapper<>();
		query.select(FwFormConfig::getId,FwFormConfig::getName,FwFormConfig::getFormCode,FwFormConfig::getLanguage);
		query.eq(FwFormConfig::getPid,confId);
		query.eq(FwFormConfig::getType,"product");
		List<FwFormConfig> lists = baseMapper.selectList(query);
		return lists;
	}

	
	@Override
	public JSONArray getTableColumns(String formId, String language) {
		LambdaQueryWrapper<FwFormConfig> query = new LambdaQueryWrapper<>();
		query.eq(FwFormConfig::getId,formId);
		query.eq(FwFormConfig::getLanguage,language);
		FwFormConfig entity = baseMapper.selectOne(query);
		String content = entity.getContent();
		List<JSONObject> contents = JSONArray.parseArray(content,JSONObject.class);
		JSONArray jsons = new JSONArray();
		contents.forEach(item ->{
			JSONObject json = new JSONObject();
			json.put("field",item.getString("field"));
			json.put("title",item.getString("title"));
			json.put("type",item.getString("type"));
			JSONObject jsonItem = new JSONObject();
			switch(item.getString("type")){
				case "input" :
					jsonItem.put("name","input");
					break;
				case "select" :
					jsonItem.put("name","$select");
					jsonItem.put("options",item.getJSONArray("options"));
					break;
			}
			json.put("editRender",jsonItem);
			jsons.add(json);
		});
		return jsons;
	}

	@Override
	public JSONArray getFeilds(String formId, String language) {
		LambdaQueryWrapper<FwFormConfig> query = new LambdaQueryWrapper<>();
		query.eq(FwFormConfig::getId,formId);
		query.eq(FwFormConfig::getLanguage,language);
		FwFormConfig entity = baseMapper.selectOne(query);
		String content = entity.getContent();
		List<JSONObject> contents = JSONArray.parseArray(content,JSONObject.class);
		JSONArray jsons = new JSONArray();
		contents.forEach(item ->{
			JSONObject json = new JSONObject();
			json.put("field",item.getString("field"));
			json.put("title",item.getString("title"));
			json.put("type",item.getString("type"));
			jsons.add(json);
		});
		return jsons;
	}
}